﻿using TrueSync;
using UnityEngine;

namespace HEFramework
{
    public static class SkillEffectLineProjectileItemSystem
    {
        [EntitySystem]
        public class SkillEffectLineProjectileItemAwakeSystem : AwakeSystem<SkillEffectLineProjectileItem, int, TSVector>
        {
            protected override void Awake(SkillEffectLineProjectileItem _self, int _a, TSVector _b)
            {
                var dc = _self.GetSceneChild<DataTableComponent>();
                if (dc == null)
                {
                    return;
                }

                var data = dc.LoadDataTable<DRSkillEffect>().GetData(_a);
                if (data == null)
                {
                    return;
                }

                _self.ID = _a;
                _self.AddChild<TransformComponent>().P = _b;
                _self.AddChild<MoveComponent>();
                _self.AddChild<SphereCollisionComponent, FP, TSVector, TSVector>(data.Radius, _b, TSVector.zero).EnterCollisionEvent = _self.OnEnter;
                _self.AddChild<SkillEffectItemDisappearComponent, FP>(data.DisappearTime);
            }
        }


        [EntitySystem]
        public class SkillEffectLineProjectileItemUpdateSystem : UpdateSystem<SkillEffectLineProjectileItem>
        {
            protected override void Update(SkillEffectLineProjectileItem _self)
            {
                var dc = _self.GetSceneChild<DataTableComponent>();
                if (dc == null)
                {
                    return;
                }

                var data = dc.LoadDataTable<DRSkillEffect>().GetData(_self.ID);
                if (data == null)
                {
                    return;
                }

                var mc = _self.GetChild<MoveComponent>();
                if (mc == null)
                {
                    return;
                }

                var np = _self.Dir + mc.APos;
                mc.SetTarget(mc.APos, np, data.Speed);
            }
        }

        public static void SetParameters(this SkillEffectLineProjectileItem _self, Entity _a, TSVector _b, TSVector _c)
        {
            _self.A = _a;
            _self.APos = _b;
            _self.BPos = _c;
            _self.Dir = (_self.BPos - _self.APos).normalized;
        }


        public static void OnEnter(this SkillEffectLineProjectileItem _self, Entity _a)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            if (dc == null)
            {
                return;
            }

            var data = dc.LoadDataTable<DRSkillEffect>().GetData(_self.ID);
            if (data == null)
            {
                return;
            }

            var a = _self.A.Entity;
            if (a == null)
            {
                return;
            }

            var sc = a.GetChild<SkillComponent>();
            if (sc == null)
            {
                return;
            }

            var tc = _self.GetChild<TransformComponent>();
            if (tc == null)
            {
                return;
            }

            sc.ExecuteSkillEffect(_self.ID, _a, tc.P);
        }
    }
}